package com.example.demo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.demo.R;
import com.example.demo.bean.FoodBean;

import java.math.BigDecimal;
import java.util.List;

public class CarAdapter extends BaseAdapter {

    private Context mContext;
    private List<FoodBean> fbl;
    private onSelectListener onSelectListener;

    public CarAdapter(Context mContext, onSelectListener onSelectListener) {
        this.mContext = mContext;
        this.onSelectListener = onSelectListener;
    }

    public void setData(List<FoodBean> fbl) {
        this.fbl = fbl;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return fbl == null? 0 : fbl.size();
    }

    @Override
    public Object getItem(int position) {
        return fbl == null? null : fbl.get(position);
    }


    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView == null){
            vh = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.car_item,null);
            vh.tv_food_name = convertView.findViewById(R.id.tv_food_name);
            vh.tv_food_count = convertView.findViewById(R.id.tv_food_count);
            vh.tv_food_price = convertView.findViewById(R.id.tv_food_price);
            vh.iv_add = convertView.findViewById(R.id.iv_add);
            vh.iv_minus = convertView.findViewById(R.id.iv_minus);
            convertView.setTag(vh);
        }else{
            vh = (ViewHolder) convertView.getTag();
        }
        final FoodBean bean = (FoodBean) getItem(position);
        if (bean != null){
            vh.tv_food_name.setText(bean.getFoolName());
            vh.tv_food_count.setText(bean.getCount()+"");
            BigDecimal count = BigDecimal.valueOf(bean.getCount());
            vh.tv_food_price.setText("￥"+bean.getPrice().multiply(count));
            vh.iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSelectListener.onSelectAdd(position,vh.tv_food_count, vh.tv_food_price);
                }
            });
            vh.iv_minus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onSelectListener.onSelectMis(position, vh.tv_food_count, vh.tv_food_price);
                }
            });
        }
        return convertView;
    }

    class ViewHolder{
        public TextView tv_food_name, tv_food_count,tv_food_price;
        public ImageView iv_add,iv_minus;
    }

    public interface onSelectListener{
        void onSelectAdd(int position,TextView tv_food_count,TextView tv_food_price);
        void onSelectMis(int position,TextView tv_food_count,TextView tv_food_price);
    }
}
